package com.example.learnvideo.ui

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.telephony.TelephonyManager
import android.util.Log
import androidx.core.app.ActivityCompat
import com.example.learnvideo.R
import com.example.learnvideo.base.BaseActivity
import com.example.learnvideo.databinding.ActivityWifiInfoBinding
import com.example.learnvideo.util.IPv4Util
import com.example.learnvideo.util.Utils

class WifiInfoActivity : BaseActivity<ActivityWifiInfoBinding>() {

    private lateinit var mConnectManager:ConnectivityManager
    private lateinit var mTelManager: TelephonyManager
    private val mWifiStateArray = arrayOf("正在断开", "已断开", "正在连接", "已连接", "未知")
    private val mHandler = Handler(Looper.getMainLooper())

    override fun ActivityWifiInfoBinding.initBinding() {
        mHandler.postDelayed(object :Runnable{
            override fun run() {
                getAvailableNet()
                mHandler.postDelayed(this,1000)
            }
        },50)
    }

    @SuppressLint("ServiceCast")
    private fun getAvailableNet(){
        var desc = ""
        mTelManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        mConnectManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info = mConnectManager.activeNetworkInfo
        if (info != null && info.state == NetworkInfo.State.CONNECTED){
            when(info.type){
                ConnectivityManager.TYPE_WIFI -> {
                    val wifiManager:WifiManager = getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
                    val wifiInfo:WifiInfo = wifiManager.connectionInfo
                    val state = wifiManager.wifiState
                    val ssid = wifiInfo.ssid
                    if (ssid == null || ssid.isEmpty() || ssid.indexOf("unknown") >= 0){
                        desc = "\n当前联网的网络类型是WIFI，但未成功连接已知的wifi信号"
                    }else{
                        if (ActivityCompat.checkSelfPermission(
                                this,
                                Manifest.permission.ACCESS_FINE_LOCATION
                            ) != PackageManager.PERMISSION_GRANTED
                        ) {
                            ActivityCompat.requestPermissions(this, arrayOf(
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.ACCESS_FINE_LOCATION,
                            ),1)
                        }
                        desc = """
                            ${desc}当前联网的网络类型是WIFI,
                            状态是${mWifiStateArray[state]}
                            WIFI名称是:${ssid}
                            路由器MAC是：${wifiInfo.bssid}
                            WIFI信号强度是：${wifiInfo.rssi}
                            连接速率是：${wifiInfo.linkSpeed}
                            手机的IP地址是：${IPv4Util.intToIp(wifiInfo.ipAddress)}
                            手机的MAC地址是：${wifiInfo.macAddress}
                            网络编号是：${wifiInfo.networkId}
                        """.trimIndent()
                    }
                }
                ConnectivityManager.TYPE_MOBILE -> {
                    val netType = info.subtype
                    desc = "\n当前联网的网络类型是${Utils.getNetworkTypeName(mTelManager,netType)} ${Utils.getClassName(mTelManager,netType)}"
                }
                else -> {
                    desc = "\n当前联网的网络类型是${info.type}"
                }
            }
        }else{
            desc = "\n当前无上网连接..."
        }
        mBinding.tvInfo.text = desc
    }
}